package com.seele.tigerwallet.ui.utils;

import com.j256.ormlite.dao.GenericRawResults;
import com.seele.base.utils.LogUtils;
import com.seele.tigerwallet.async.Log;
import com.seele.tigerwallet.model.AssetsModel;
import com.seele.tigerwallet.model.UserInfo;
import com.seele.tigerwallet.ui.manager.DaoManager;
import com.seele.tigerwallet.ui.orm.GenericRowMapper;

import java.sql.SQLException;
import java.util.List;

public class SqlUtil {


    public static String queryAssetsFromAsc() {

        return "SELECT * FROM t_assets  where point='1' and isOpen='1' ORDER BY postion ASC ";
    }


    public static String queryAssetsFromPoint() {

        return "SELECT * FROM t_assets  where point='2' ";
    }

    public static String queryMarketFromAsc() {

        return "SELECT * FROM t_market ORDER BY position ASC";
    }


    public static void updateState(int isUpload, String address) {
        try {
            DaoManager.getTokenDao().updateRaw(getSql(isUpload, address));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static String getSql(int isUpload, String address) {
        String sql = String.format("UPDATE t_token SET isUpload = '%d' WHERE address ='%s'", isUpload, address);
        LogUtils.d(sql);
        return sql;
    }


    public static String updateContact(String toaddr, String address, String firstname, String lastname, String mail, String note, String phone, String contactsId) {
        String sql2 = String.format("UPDATE t_contacts SET toaddr = '%s',  address = '%s', firstname = '%s', lastname = '%s', mail = '%s', note = '%s  ,, phone = '%s WHERE contactsId ='%s'", toaddr, address, firstname, lastname, mail, note, phone, contactsId);

        return sql2;
    }


    /**
     * 查询资产
     *
     * @param sql
     * @return
     */
    public static List<AssetsModel> getAssetsModel(String sql) {
        GenericRawResults<AssetsModel> rawResults = null;
        List<AssetsModel> assetsModelList = null;
        try {
            rawResults = DaoManager.getAssetsDao().queryRaw(sql, new GenericRowMapper(AssetsModel.class));
            assetsModelList = rawResults.getResults();
            rawResults.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return assetsModelList;

    }


}